package cn.iocoder.yudao.module.written.exam.controller.admin.jobcategories;

import cn.iocoder.yudao.module.written.exam.controller.admin.jobcategories.vo.JobCategoriesPageReqVO;
import cn.iocoder.yudao.module.written.exam.controller.admin.jobcategories.vo.JobCategoriesRespVO;
import cn.iocoder.yudao.module.written.exam.controller.admin.jobcategories.vo.JobCategoriesSaveReqVO;
import cn.iocoder.yudao.module.written.exam.dal.dataobject.jobcategories.JobCategoriesDO;
import cn.iocoder.yudao.module.written.exam.service.jobcategories.JobCategoriesService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

@Tag(name = "管理后台 - 职位分类")
@RestController
@RequestMapping("/written-exam/job-categories")
@Validated
public class JobCategoriesController {

    @Resource
    private JobCategoriesService jobCategoriesService;

    @PostMapping("/create")
    @Operation(summary = "创建职位分类")
    @PreAuthorize("@ss.hasPermission('written-exam:job-categories:create')")
    public CommonResult<Long> createJobCategories(@Valid @RequestBody JobCategoriesSaveReqVO createReqVO) {
        return success(jobCategoriesService.createJobCategories(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新职位分类")
    @PreAuthorize("@ss.hasPermission('written-exam:job-categories:update')")
    public CommonResult<Boolean> updateJobCategories(@Valid @RequestBody JobCategoriesSaveReqVO updateReqVO) {
        jobCategoriesService.updateJobCategories(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除职位分类")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('written-exam:job-categories:delete')")
    public CommonResult<Boolean> deleteJobCategories(@RequestParam("id") Long id) {
        jobCategoriesService.deleteJobCategories(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得职位分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('written-exam:job-categories:query')")
    public CommonResult<JobCategoriesRespVO> getJobCategories(@RequestParam("id") Long id) {
        JobCategoriesDO jobCategories = jobCategoriesService.getJobCategories(id);
        return success(BeanUtils.toBean(jobCategories, JobCategoriesRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得职位分类分页")
    @PreAuthorize("@ss.hasPermission('written-exam:job-categories:query')")
    public CommonResult<PageResult<JobCategoriesRespVO>> getJobCategoriesPage(@Valid JobCategoriesPageReqVO pageReqVO) {
        PageResult<JobCategoriesDO> pageResult = jobCategoriesService.getJobCategoriesPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, JobCategoriesRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出职位分类 Excel")
    @PreAuthorize("@ss.hasPermission('written-exam:job-categories:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportJobCategoriesExcel(@Valid JobCategoriesPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<JobCategoriesDO> list = jobCategoriesService.getJobCategoriesPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "职位分类.xls", "数据", JobCategoriesRespVO.class,
                        BeanUtils.toBean(list, JobCategoriesRespVO.class));
    }

}